<?php
namespace app\common\repositories\system;
use app\common\repositories\BaseRepository;
use app\common\dao\system\AdminLogDao as dao;
use app\Request;


/**
 * @mixin dao
 */
class AdminLogRepository extends BaseRepository
{
    public function __construct(dao $dao)
    {
        $this->dao = $dao;
    }

    public function lst(array $where, $page, $limit)
    {
        $query = $this->dao->search($where);
        $count = $query->count($this->dao->getPk());
        $list = $query->setOption('field', [])->field(['create_time', 'log_id', 'admin_name', 'route', 'method', 'url', 'ip', 'admin_id'])
            ->page($page, $limit)->order('create_time DESC')->select();
        return compact('count', 'list');
    }


    public function addLog(Request $request)
    {
        return $this->create(self::parse($request));
    }


    public function create(array $data)
    {
        return $this->dao->create($data);
    }

    public static function parse(Request $request)
    {
        return [
            'admin_id' => $request->adminId(),
            'admin_name' => $request->adminInfo()->real_name ?: '未定义',
            'route' => $request->rule()->getName(),
            'ip' => $request->ip(),
            'url' => $request->url(true),
            'method' => $request->method()
        ];
    }

}